package com.iary.interfaces.web;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iary.application.TaokeService;
import com.iary.application.TbShopService;
import com.iary.infrastructure.Page;
import com.iary.infrastructure.QueryCriteria;
import com.taobao.api.domain.Shop;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.domain.TaobaokeItemDetail;

@Controller
public class SearchController {

	private static final Logger log = LoggerFactory
			.getLogger(SearchController.class);

	@Autowired
	private TaokeService taokeService;

	@Autowired
	private TbShopService tbShopService;

	@RequestMapping("/search")
	public ModelAndView execute(@RequestParam() String keyword,
			@RequestParam(required = false) Integer pageNo,
			@RequestParam(required = false) String type) {
		log.info("Search keyword : " + keyword);

		ModelAndView mav = new ModelAndView();
		Page<TaobaokeItem> page = new Page<TaobaokeItem>(40);
		if (pageNo != null) {
			page.setPageNo(pageNo.intValue());
		}
		if (type == null || (!type.equals("0") && (!type.equals("1")))) {
			type = "0";
		}
		QueryCriteria criteria = new QueryCriteria(keyword);
		if (type.equals("0")) {
			page = taokeService.searchTbkItems(criteria, page);
			mav.setViewName("search-list");
		}
		if (type.equals("1")) {
			page = taokeService.searchTbkItems(criteria, page);
			mav.setViewName("search-list");
		}

		mav.addObject("keyword", keyword);
		mav.addObject("type", type);
		mav.addObject("page", page);
		return mav;
	}

	@RequestMapping("/shop/{itemId}")
	public String shop(@PathVariable Long itemId) {
		TaobaokeItemDetail item = taokeService.getTbkItemLink(itemId);
		if (item != null && item.getShopClickUrl() != null) {
			return "redirect:" + item.getShopClickUrl();
		}
		return null;
	}

	@RequestMapping("/buy/{itemId}")
	public String buy(@PathVariable Long itemId) {
		TaobaokeItemDetail item = taokeService.getTbkItemLink(itemId);
		if (item != null && item.getShopClickUrl() != null) {
			return "redirect:" + item.getClickUrl();
		}
		return null;
	}

	@RequestMapping("/item/{itemId}")
	public ModelAndView item(@PathVariable Long itemId) {
		TaobaokeItemDetail item = taokeService.getTbkItemDetail(itemId);
		ModelAndView mav = new ModelAndView("item-detail");
		if (item == null || item.getItem() == null) {
			return null;
		}

		mav.addObject("itemResult", item);

		Shop shop = null;

		shop = tbShopService.getShopByNick(item.getItem().getNick());
		mav.addObject("shopResult", shop);

		return mav;
	}

}
